﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
#region

using System.Linq;
using System.Web.Mvc;
using BTL.OnlineTestingSocial.Application.Facade;
using BTL.OnlineTestingSocial.Application.Web.Infrastructure.ViewModels;

#endregion

namespace BTL.OnlineTestingSocial.Application.Web.Controllers
{
    public class HomeController : Controller
    {
        private readonly IOnlineTestingEngineFacade _onlineTestingFacade;

        public HomeController()
            : this(DependencyResolver.Current.GetService<IOnlineTestingEngineFacade>())
        {
        }

        public HomeController(IOnlineTestingEngineFacade onlineTestingFacade)
        {
            _onlineTestingFacade = onlineTestingFacade;
        }

        public ActionResult Index()
        {
            var questGroup = _onlineTestingFacade.GetPublicQuestionGroups(1, 10, 1).ToList();
            ViewData["isinit"] = true;
            if (questGroup != null && questGroup.Count > 0)
            {
                ViewData["PageIndex"] = 1;
                ViewData["isgetNextPage"] = true;
            }
            else
            {
                ViewData["PageIndex"] = 0;
                ViewData["isgetNextPage"] = false;
            }
            return View(questGroup);
        }

        public PartialViewResult getMoreQuestionGroup(FormCollection frm,string kind)
        {
            int pageIndex = 0;
            ViewData["PageIndex"] = pageIndex;
            if (!string.IsNullOrEmpty(frm["PageIndex"]) && int.TryParse(frm["PageIndex"], out pageIndex) && pageIndex +1 >1)
            {
                pageIndex++;
                var questGroup = _onlineTestingFacade.GetPublicQuestionGroups(1, 10, pageIndex).ToList();
                if (questGroup != null && questGroup.Count > 0)
                {
                    ViewData["isgetNextPage"] = true;
                }
                else
                    ViewData["isgetNextPage"] = false;
                
                ViewData["PageIndex"] = pageIndex;
                return PartialView("pvwQuestionGroupItem",questGroup);
            }
            
            return PartialView(null);
        }

    }
}
